<?php

class MySpaceUtils
{
 /**
   *
   * @param $json a java script object
   *
   * @return Object - a PHP object that represents the JSON
   * */
  public static function parseJSON($json) {      
  	if(gettype($json)=="object"){
  		return $json;
  	}

    $r = json_decode($json);

    if (isset($r->rsp) && $r->rsp->stat != 'ok') {
	    throw new MySpaceException(
    								$r->rsp->code.": ".$r->rsp->message,
    								MySpaceException::REMOTE_ERROR,
    								$r->rsp
    							);
    }
    return $r;
  }
  
  /**
   *
   * @param $json a java script object for elements starting with Array
   *
   * @return Object - a PHP object that represents the JSON
   **/
  public static function parseJSONArray($json) {      
  	if(gettype($json)=="object"){
  		return $json;
  	}

    $r = json_decode('{"array":'.$json.'}');

    if (isset($r->rsp) && $r->rsp->stat != 'ok') {
	    throw new MySpaceException(
    								$r->rsp->code.": ".$r->rsp->message,
    								MySpaceException::REMOTE_ERROR,
    								$r->rsp
    							);
    }
    return $r;
  }
}

?>
